package org.mch.func.file.filedelete;

import java.awt.event.ActionEvent;
import java.io.File;

import org.mch.biz.ui.FuncEvent;
import org.mch.biz.ui.FuncPanel;

/**
 * 描述：文件操作；
 * 
 * @author MCH；Date：2008-10-01；<BR/>
 * 
 */
public class ClientEvent extends FuncEvent {
	private static final long serialVersionUID = -8590108060646853821L;

	public ClientEvent(FuncPanel panel) {
		super(panel);
	}

	protected void onButtonClick(ActionEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_execute()) {
			execute();
		}
	}

	public void execute() {
		String[] delfilelist = getPanel().getText_filename().getText().split("\n");
		String topath = getPanel().getText_rootpath().getValue();

		File[] files = new File(topath).listFiles();
		deleteFiles(files, delfilelist);
	}

	private void deleteFiles(File[] files, String[] delfilelist) {
		for (File f : files) {
			if (f.isFile()) {
				for (String name : delfilelist) {
					if (f.getName().equals(name)) {
						f.delete();
						break;
					}
				}
			} else if (f.isDirectory()) {
				deleteFiles(f.listFiles(), delfilelist);
			}
		}

	}

	public ClientPanel getPanel() {
		return (ClientPanel) super.getPanel();
	}
}